/*
 * Created by SharpDevelop.
 * User: Rogerio
 * Date: 18/11/2008
 * Time: 10:38
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.Data;
using csharp.common;
using System.Collections.Generic;

namespace csharp.rad.finan
{
	/// <summary>
	/// Description of Group.
	/// </summary>
	public class Bill : DefaultClass, IClass
	{
		private String   _code;
		private String   _name;
		private DateTime _register_date;
		private bool     _active;
		private String   _subgroup_id;
		
		public Bill()
		{
			gsql.table= "finan.bill";
			_sequence = "finan.bill_bill_id_seq";
		}
		
		public String code
		{
			set { _code = value; }
			get { return  _code; }
		}
		
		public String name
		{
			set { _name = value; }
			get { return  _name; }
		}
		
		public DateTime registerDate
		{
			set { _register_date = value; }
			get { return  _register_date; }
		}
		
		public bool active
		{
			set { _active = value; }
			get { return  _active; }
		}
		
		public String subgroupId
		{
			set { _subgroup_id = value; }
			get { return  _subgroup_id; }
		}
		
		public void clear()
		{
			_id      		= String.Empty;
			_code    		= String.Empty;
			_name				= String.Empty;
			_register_date = DateTime.Now;
			_active  		= true;
			_subgroup_id 	= String.Empty;
		}
		
	
	
		/// <summary>
		/// Fill the bill with data of DataRow
		/// </summary>
		/// <param name="dr">DataRow with the data to fill the bill.</param>				
		public override void get(DataRow dr)
		{
			_id       		= dr["bill_id"].ToString();
			_code     		= dr["bill_code"].ToString();
			_name     		= dr["bill_name"].ToString();
			_register_date = Convert.ToDateTime(dr["bill_register_date"]);
			_active   		= Convert.ToBoolean(dr["bill_active"]);  
			_subgroup_id      = dr["bill_subgroup_id"].ToString();
		}
		
		
		public override void setGsql()
		{
			gsql.setValue(_id, "bill_id");
			gsql.setValue(_code, "bill_code");
			gsql.setValue(_name, "bill_name");
			//gsql.setValue(_register_date.ToString("dd/MM/yyyy"), "group_register_date");
			gsql.setValue(_active.ToString(), "bill_active");
			gsql.setValue(_subgroup_id, "bill_subgroup_id");
		}
	}


	//List
	public class Bills : DefaultList, IList
	{
		//private List<Bill> items = new List<Bill>();
		//private List<Bill> _normal = new List<Bill>();
		//private List<Bill> _filtered = new List<Bill>();
		
		public Bills()
		{
			gsql.table= "finan.v_bill";
		}
		
		public Bill get(Int32 index)
		{
			return (Bill)items[index];
		}
		
		public Bill get()
		{
			return (Bill)items[index];
		}
		
		public bool fillList()
		{
			gsql.prepare();
			gsql.setValue("","*");
			gsql.additionalWhere = "ORDER BY bill_code";
			
			return mountList();
		}
		
		public bool fillList(String subGroupId)
		{
			gsql.prepare();
			gsql.setValue("","*");
			gsql.conditionalWhere = "bill_subgroup_id = "+subGroupId;
			gsql.additionalWhere = "ORDER BY bill_code";
			
			return mountList();
		}		
		
		private bool mountList()
		{
			Int32 tam;
			
			if (!gsql.execute())
				return false;
			
			tam= gsql.getRowCount();
			clear();
			for (int i=0; i < tam; i++)
			{
				Bill g = new Bill();
				g.get(gsql.getRow(i));
				items.Add(g);
			}
			return true;
		}
		
		
		public bool searchByName(String name)
		{
			int tam= items.Count;
			
			for (int i=0; i < tam; i++)
			{
				if ( ((Bill)items[i]).name.Equals(name))
				{
					index= i;
					return true;
				}
			}
			return false;
		}		
		
		public void filterSubGroup(string subGroupId)
		{
			int tam = items.Count;
			int i;
			
			filtered.Clear();
			for(i=0; i < tam; i++)
			{
				if (((Bill)items[i]).subgroupId.Equals(subGroupId))
				{
					filtered.Add(items[i]);
				}
			}
			items = filtered;
		}
	}
}


